/*
 * @(#) LegalDaysServiceImpl.java 2011-10-2
 *
 * Copyright (c) 2011, Strong Technology. All Rights Reserved.
 * Strong Technology. CONFIDENTIAL
 */
package cn.strong.gweva.system.business.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import cn.strong.gweva.common.business.entity.Environment;
import cn.strong.gweva.common.business.entity.db.JxkhLegalDaysEntity;
import cn.strong.gweva.common.business.service.impl.AbstractServiceImpl;
import cn.strong.gweva.common.util.Constants;
import cn.strong.gweva.common.util.Constants4Method;
import cn.strong.gweva.system.business.service.LegalDaysService;

/**
 * 法定天数管理业务处理。
 * 
 * @author lilanfei
 * @version 1.0
 * @since 2011-10-2
 */
public class LegalDaysServiceImpl extends AbstractServiceImpl implements LegalDaysService
{
	/**
	 * 检索法定天数列表。
	 * 
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public List<JxkhLegalDaysEntity> searchLegalDaysList() throws Exception
	{
		return this.getCommonDao().searchList(Constants4Method.SYSTEM_LEGAL_DAYS_SEARCH_LEGAL_DAYS_LIST, null);
	}
	
	/**
	 * 检索法定天数列表（计算用）。
	 * 
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public List<JxkhLegalDaysEntity> searchLegalDaysList4Compute() throws Exception
	{
		return this.getCommonDao().searchList(Constants4Method.SYSTEM_LEGAL_DAYS_SEARCH_LEGAL_DAYS_LIST_4_COMPUTE, null);
	}
	
	/**
	 * 更新法定天数列表。
	 * 
	 * @param legalDaysList 现法定天数列表
	 * @param legalDaysIds4Del 要删除的法定天数ID集合
	 * @param LoginUserId 登录用户编号
	 * @throws Exception
	 */
	@SuppressWarnings("unchecked")
	public void updateLegalDaysList(List<JxkhLegalDaysEntity> legalDaysList, Integer LoginUserId) throws Exception
	{
		if (legalDaysList != null && legalDaysList.size() > 0)
		{
			List<JxkhLegalDaysEntity> insertList = new ArrayList<JxkhLegalDaysEntity>();
			List<JxkhLegalDaysEntity> updateList = new ArrayList<JxkhLegalDaysEntity>();
			for (JxkhLegalDaysEntity legalDays : legalDaysList)
			{
				if (legalDays.getRangeType() == null || legalDays.getYears() == null || legalDays.getDays() == null)
				{
					continue;
				}
				legalDays.setUpdateTime(new Date());
				legalDays.setUpdateUserId(LoginUserId);
				if (legalDays.getLegalDaysId() == null)
				{
					legalDays.setDelFlag(Constants.DEL_FLAG_NO);
					insertList.add(legalDays);
				}
				else
				{
					updateList.add(legalDays);
				}
			}
			
			if (insertList.size() > 0)
			{
				this.getCommonDao().insertBatch(Constants4Method.SYSTEM_LEGAL_DAYS_INSERT_LEGAL_DAYS, insertList);
			}

			if (updateList.size() > 0)
			{
				this.getCommonDao().updateBatch(Constants4Method.SYSTEM_LEGAL_DAYS_UPDATE_LEGAL_DAYS, updateList);
			}
			
			// 刷新环境变量
			Environment.refreshLegalDaysList(
					this.getCommonDao().searchList(Constants4Method.SYSTEM_LEGAL_DAYS_SEARCH_LEGAL_DAYS_LIST_4_COMPUTE, null));
		}
	}

	/**
	 * 根据法定天数ID，删除法定天数。
	 * 
	 * @param legalDaysId 法定天数ID
	 * @return
	 * @throws Exception
	 */
	@SuppressWarnings("unchecked")
	public int deleteById(Integer legalDaysId) throws Exception
	{
		int cnt = this.getCommonDao().delete(Constants4Method.SYSTEM_LEGAL_DAYS_DELETE_LEGAL_DAYS_BY_ID, legalDaysId);
		
		// 刷新环境变量
		Environment.refreshLegalDaysList(
				this.getCommonDao().searchList(Constants4Method.SYSTEM_LEGAL_DAYS_SEARCH_LEGAL_DAYS_LIST_4_COMPUTE, null));
		
		return cnt;
	}
}
